package com.jewelry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jewelry.entity.Product;
import com.jewelry.service.ProductService;
import com.jewelry.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LZL
 * @description 针对表【t_product】的数据库操作Service实现
 * @createDate 2024-03-21 20:53:04
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> selectBiaJiTuiJia() {
        return productMapper.selectBiaJiTuiJia();
    }

    @Override
    public List<Product> selectHot() {
        return productMapper.selectHotBooks();
    }

    @Override
    public List<Product> selectLessStock() {
        return productMapper.selectLessStockBooks();
    }

    @Override
    public List<Product> selectLessPrice() {
        return productMapper.selectLessPriceBooks();
    }

    @Override
    public Map selectProByNameByPage(Integer pageNum, Integer pageSize, String productName) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name", productName);
        Page<Product> page = new Page<>(pageNum, pageSize); // pageNum 是当前页码，pageSize 是每页显示的数据条数
        System.out.println("=======================================" + page);
        IPage<Product> productPage = productMapper.selectPage(page, queryWrapper);
        List<Product> productList = productPage.getRecords();
        System.out.println("productList = " + productList);// 当前页的数据
        long total = productPage.getTotal();// 总条数
        System.out.println("噢噢噢噢total = " + total);
        long pages = productPage.getPages();// 总页数
        System.out.println("哈哈哈哈pages = " + pages);
        Map map = new HashMap<>();
        map.put("list", productList);
        map.put("totalRows", total);
        map.put("totalPages", pages);

        return map;
    }

    @Override
    public Page<Product> getProductByCategory(Integer pId, Integer cId, Integer pageNum, Integer pageSize) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        return productMapper.selectBuCategory(page, pId, cId);
    }
}




